﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace _4DJackPot
{

    public partial class Ini
    {
        // INI Read Write를 위한 API 선언
        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(    // INI Read
            String section,
            String key,
            String def,
            StringBuilder retVal,
            int size,
            String filePath);
        [DllImport("kernel32.dll")]
        private static extern long WritePrivateProfileString(  // INI Write
            String section,
            String key,
            String val,
            String filePath);

        private string defaultFileDirectory = System.Windows.Forms.Application.StartupPath + "\\Data";
        private string defaultFileName = "NextSPC.ini";

        public Ini()
        {

        }

        // Default Ini File Read
        public string DefaultFileReadIni(string sectionString, string keyString)
        {
            System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(defaultFileDirectory);
            if (directoryInfo.Exists == false)
            {
                directoryInfo.Create();
            }
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(defaultFileDirectory + "\\" + defaultFileName);
            if (fileInfo.Exists == false)
            {
                fileInfo.Create();
            }

            return ReadIni(sectionString, keyString, defaultFileDirectory + "\\" + defaultFileName);
        }

        // Default Ini File Write
        public void DefaultFileWriteIni(string sectionString, string keyString, string dataString)
        {
            System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(defaultFileDirectory);
            if (directoryInfo.Exists == false)
            {
                directoryInfo.Create();
            }
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(defaultFileDirectory + "\\" + defaultFileName);
            if (fileInfo.Exists == false)
            {
                fileInfo.Create();
            }

            WriteIni(
                sectionString, keyString,
                dataString,
                defaultFileDirectory + "\\" + defaultFileName);
        }
        // Write (Section, Key, Data, File Path)
        public void WriteIni(
            string sectionString, string keyString, string dataString, string filePathString)
        {
            WritePrivateProfileString(sectionString, keyString, dataString, filePathString);
        }
        // Write (Section, Key, Data)
        public void WriteIni(string sectionString, string keyString, string dataString)
        {
            WritePrivateProfileString(sectionString, keyString, dataString, defaultFileDirectory + "\\" + defaultFileName);
        }
        // Read (Section, Key, File Path)
        public string ReadIni(string sectionString, string keyString, string filePathString)
        {
            StringBuilder temp = new StringBuilder(200000);
            int i = GetPrivateProfileString(sectionString, keyString, "", temp, temp.Capacity, filePathString);
            return temp.ToString();
        }
        // Read (Section, Key)
        public string ReadIni(string sectionString, string keyString)
        {
            StringBuilder temp = new StringBuilder(200000);
            int i = GetPrivateProfileString(sectionString, keyString, "", temp, temp.Capacity, defaultFileDirectory + "\\" + defaultFileName);
            return temp.ToString();
        }
    }

}
